<template>
    <div>这是详情页</div>
</template>
<script>
export default{
    data(){
        return{
            starttime:'',
            endtime:'',
            shoppitem:'' ,  //点击商品块的详细信息
            browseTime: 0,  // 浏览时长初始值为 0
            clearTimeSet: null, 
        }
    },
    methods:{
        setTime() {
     //设置定时器
     this.clearTimeSet = setInterval(() => {
       this.browseTime++;
       console.log(this.browseTime, "时长累计");
     }, 1000);
   },
        //浏览商品插入浏览记录表的方法
        BrowsingHistory(){      
            this.$axios({
                url:'/api/browsingTableinsert/get',
                method:'get',
                params:{
                    shoppid:this.shoppitem.commodityId,
                    userid:111,     //用户
                    statime:this.starttime,
                    endtime:this.endtime 
                },
            })
            .then(resp=>{
                console.log(resp.data)
            })
            .catch(err=>{
                    console.log(err.data)
                })
        },
    },
    mounted(){
        this.starttime = new Date().valueOf()    //进入页面就生成一个时间戳
        console.log('this.shoppitem')
        //接收路由传参的参数
        let spxx = this.$route.params.id
        this.shoppitem = spxx
        console.log(spxx,20)
        console.log(this.shoppitem,21)
        

        // document.addEventListener('visibilitychange',function(e){
        //     console.log(document.visibilityState,e);
        //     let state = document.visibilityState
        //     if(state == 'hidden'){
        //         console.log(document.visibilityState,'用户离开了');
        //     }
        //     if(state == 'visible'){
        //         console.log(document.visibilityState,'回来了');
        //         let t1 = new Date().valueOf()
        //         console.log(t1);
        //     }
        // });
    },
    beforeDestroy(){
        //销毁离开页面时生成一个时间戳
        this.endtime = new Date().valueOf()   
        console.log(this.endtime,this.starttime,this.endtime-this.starttime,70)

        //如果停留的时间和离开的时间差大于6秒，就执行插入浏览记录的方法
        if(this.endtime-this.starttime>=6000){     
            this.BrowsingHistory()
        }
    },
}
</script>